package safeCommitter.core.template;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import safeCommitter.core.view.main.ISCTestRunListener;
import safeCommitter.test.report.SCClazzTestResult;
import safeCommitter.test.report.SCTestResult;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "InovacaoTeste";

	// The shared instance
	private static Activator plugin;

	private JavaProject currentProject;

	private final List<ISCTestRunListener> listeners = new LinkedList<ISCTestRunListener>();

	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void start(final BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void stop(final BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(final String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public void addTestListener(final ISCTestRunListener listener) {
		listeners.add(listener);
	}

	public void removeTestListener(final ISCTestRunListener listener) {
		listeners.remove(listener);
	}

	public void fireTestsStarted(final int clazzCount, Integer testCount) {
		for (Iterator<ISCTestRunListener> all = getListeners().iterator(); all
				.hasNext();) {
			ISCTestRunListener each = all.next();
			each.testsStarted(clazzCount, testCount);
		}
	}

	public void fireTestsFinished() {
		for (Iterator<ISCTestRunListener> all = getListeners().iterator(); all
				.hasNext();) {
			ISCTestRunListener each = all.next();
			each.testsFinished();
		}
	}

	public void fireTestResult(final SCTestResult result) {
		for (Iterator<ISCTestRunListener> all = getListeners().iterator(); all
				.hasNext();) {
			ISCTestRunListener each = all.next();
			each.testResult(result);
		}
	}

	protected List<ISCTestRunListener> getListeners() {
		return listeners;
	}

	/**
	 * Updates the view(s) informing that a new test class has been processed.
	 * 
	 * @param clazzResult
	 */
	public void firenewClazzResult(final SCClazzTestResult clazzResult) {
		for (Iterator<ISCTestRunListener> all = getListeners().iterator(); all
				.hasNext();) {
			ISCTestRunListener each = all.next();
			each.addNewClazz(clazzResult);
		}

	}

	public JavaProject getJavaProject() {
		return currentProject;
	}

	public void setCurrentProject(final JavaProject currentProject) {
		this.currentProject = currentProject;
	}

	public void updateTestCount(final int clazzCount, Integer testCount) {
		for (Iterator<ISCTestRunListener> all = getListeners().iterator(); all
				.hasNext();) {
			ISCTestRunListener each = all.next();
			each.updateTestCount(clazzCount, testCount);
		}

	}

	public void updateStats(Integer successCount, Integer failCount,
			Integer skippCount) {
		for (Iterator<ISCTestRunListener> all = getListeners().iterator(); all
				.hasNext();) {
			ISCTestRunListener each = all.next();
			each.updateStats(successCount, failCount, skippCount);
		}

	}
}
